package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.Usermanage;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;

    /**
     * 会员信息分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//查询条件
        //完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage, pageSize);
        //select * from t_checkitem limit 0,10
        Page<CheckItem> page = memberDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        return new PageResult(total, rows);
    }

    /**
     * 新增会员信息
     *
     * @param member
     */
    @Override
    public void add(Member member) {
        member.setRegTime(new Date());
        memberDao.add(member);
    }

    /**
     * 编辑会员信息
     *
     * @param member
     */
    @Override
    public void edit(Member member) {
        memberDao.edit(member);
    }

    /**
     * 删除会员信息
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);
    }

    @Override
    public Usermanage findById(Integer valueOf) {
        Usermanage usermanage = memberDao.selectById(valueOf);
        return usermanage;
    }

    @Override
    public List<String> findAddress(Integer memberId) {
        try {
            //先去查询order表获取id
            List<Integer> integers = memberDao.findId(memberId);
            List<String> addressList = new ArrayList<>();
            for (Integer integer : integers) {
               String address =  memberDao.findAddress(integer);
               if (address==null||address.length()==0){
                   continue;
               }
               addressList.add(address);
            }
            return addressList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Setmeal> findSetmealNameByMemberId(Integer memberId) {
        //查询套餐id
       List<Integer> setmealList =  memberDao.findSetmealId(memberId);
       //查询套餐及其所有
        List<Setmeal> setmeals = new ArrayList<>();
        for (Integer integer : setmealList) {
            Setmeal setmeal = memberDao.findSetmealByMemberId(integer);
            setmeals.add(setmeal);
        }
        return setmeals;
    }


}
